const TerserPlugin = require('terser-webpack-plugin');
const JavaScriptObfuscator = require('webpack-obfuscator');

module.exports = {
  productionSourceMap: false,
  configureWebpack: {
    optimization: {
      minimizer: [
        new TerserPlugin({
          terserOptions: {
            compress: {
              drop_console: true,
              drop_debugger: true
            },
            mangle: {
              safari10: true
            },
            output: {
              comments: false
            }
          },
          extractComments: false,
          parallel: true
        })
      ]
    },
    plugins: [
      // 仅在生产环境使用代码混淆
      ...(process.env.NODE_ENV === 'development' ? [
        new JavaScriptObfuscator({
          rotateStringArray: true,
          shuffleStringArray: true,
          stringArray: true,
          stringArrayEncoding: ['base64'],
          stringArrayThreshold: 0.8,
          transformObjectKeys: true,
          unicodeEscapeSequence: false
        })
      ] : [])
    ]
  },
  chainWebpack: config => {
    if (process.env.NODE_ENV === 'development') {
      config.plugins.delete('prefetch');
      config.optimization.minimize(true);
    }
  }
};